﻿Public Class ucwinccsetting
    Implements IConfHolder
    Private validationerror As String
    Private wcsetting As WinccConfig
    Private bwcsetting As WinccConfig
    

    Public Function applyConfig() As Boolean Implements IConfHolder.applyConfig
        copysetting()
        If (Not wcsetting.checkConfig) Then
            validationerrors = wcsetting.ValidationErrors
            Return False
        End If
        wcsetting.writeConfig()
        Return True
    End Function

    Public Sub forcewrite() Implements IConfHolder.forcewrite
        copysetting()
        wcsetting.writeConfig()
    End Sub

    Public Sub loadConfig() Implements IConfHolder.loadConfig
        wcsetting = New WinccConfig
        bwcsetting = New WinccConfig
        displaySetting()
    End Sub

    Public Sub revertconfig() Implements IConfHolder.revertconfig
        wcsetting.Catalog = bwcsetting.Catalog
        wcsetting.Datasource = bwcsetting.Datasource
        wcsetting.RequireLogin = bwcsetting.RequireLogin
        wcsetting.LoginName = bwcsetting.LoginName
        wcsetting.LoginPswd = bwcsetting.LoginPswd
        displaySetting()
    End Sub

    Public Property validationerrors As String Implements IConfHolder.validationerrors
        Get
            Return validationerror
        End Get
        Set(ByVal value As String)
            validationerror = value
        End Set
    End Property
    Private Sub copysetting()
        wcsetting.LoginName = txtusername.Text
        wcsetting.LoginPswd = txtpswd.Text
        wcsetting.RequireLogin = chklogin.Checked
        wcsetting.Catalog = txtcatalog.Text
        wcsetting.Datasource = cmbdts.Text
        wcsetting.AlarmView = cmbalarmview.SelectedItem
    End Sub
    Private Sub displaySetting()

        loadServer()
        txtcatalog.Text = wcsetting.Catalog
        'txtdts.Text = wcsetting.Datasource
        txtpswd.Text = wcsetting.LoginPswd
        txtusername.Text = wcsetting.LoginName
        chklogin.Checked = wcsetting.RequireLogin
        cmbalarmview.SelectedItem = wcsetting.AlarmView
    End Sub
    Private Sub loadServer()
        Dim dt As DataTable = Sql.SqlDataSourceEnumerator.Instance.GetDataSources
        Dim b As Boolean = False
        cmbdts.Items.Clear()
        For Each r As DataRow In dt.Rows
            Dim srv As String = r.Item("ServerName")
            Dim sq As String = ""
            If (Not r.IsNull("InstanceName")) Then
                sq = r.Item("InstanceName")
                Dim sqi = srv + "\" + sq
                cmbdts.Items.Add(sqi)
                If (sqi = wcsetting.Datasource) Then b = True
            End If
           
        Next
        If (Not b) Then cmbdts.Items.Add(wcsetting.Datasource)
        cmbdts.SelectedItem = wcsetting.Datasource
    End Sub
    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub
End Class
